package com.store.serviceimpl;

import com.store.mapper.CategoryMapper;
import com.store.pojo.Category;
import com.store.service.CategoryService;
import com.store.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;


    @Override
    public List<Category> findAllCategoryInfo() {
        List<Category> categoryList = categoryMapper.findAllCategoryInfo();
        return categoryList;
    }

    @Override
    public Map<String, Object> addCategoryInfo(Category category) {
        Map<String,Object> map = new HashMap<>();
        int row = categoryMapper.addCategory(category);
        if (row!=1){
            map.put("state",201);
            map.put("msg","添加失败");
            return map;
        }
        map.put("state",200);
        map.put("msg","添加成功");
        return map;
    }

    @Override
    public int deleteCategoryInfoById(int id) {
        //对参数进行判断
        if (id==0){
            throw new RuntimeException("id不能空");
        }
        //使用持久层对象调用持久层方法
        int row = categoryMapper.deleteCategoryById(id);
        return row;
    }

    @Override
    public Category findCategoryById(Integer id) {
        if (id==null){
            throw  new RuntimeException("id不能为空");
        }
        Category category = categoryMapper.findCategoryById(id);
        return category;
    }

    @Override
    public int updateCategory(Category category) {
        int row = categoryMapper.toUpdateCategoryById(category);
        return row;
    }


}

